/*
 * Robust Pointers - A framework for robust pointers in a MT environment.
 * http://code.google.com/p/rptr/
 *
 * Copyright (c) 2009, Bill K. Barekas <bbarekas@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *    1. Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *    3. Neither the name of the author nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * @file    rptr_error.c
 * @author  Bill K. Barekas <bbarekas@gmail.com>
 * @date    October 10, 2009
 * @version $Id$
 *
 * This file contains the error string definitions.
 */


#include <string.h>
#include <errno.h>

#include "rptr_error.h"


/**
 * rptr_errno
 *
 * Global RPTR last error returned.
 */
int                 rptr_errno = 0;


/**
 * rptr_error_array_str
 *
 * This array defines the sting descriptions for the various error codes.
 */
static char *
rptr_error_array_str[RPTR_ERROR_MAX+1] =
{
  "Success",                            /* RPTR_OK */
  "Common error, use errno",            /* RPTR_ERROR_COMMON */
  "Already initialized",                /* RPTR_ERROR_INITIALIZED */
  "Not initialized",                    /* RPTR_ERROR_NOT_INITIALIZED */
  "Invalid RPTR type",                  /* RPTR_ERROR_INVALID_RPTR_TYPE */
  "RPTR obj status not allocated",      /* RPTR_ERROR_OBJ_NOT_ALLOCATED */
  /**
   * NOTE:
   * When add new values here, update rptr_error_e enumeration.
   */
  "Unknown error",                      /* When out-of-bounds */
};


/**
 * rptr_error_str
 *
 * This function returns the error description string for the specified
 * error code.
 *
 * @param   error error code under query
 * @return  a string describing the error
 */
char *
rptr_error_str(int error)
{
  /* Bound the error into the known range [-RPTR_ERROR_MAX, 0]. */
  if ((error > 0) || (error <= -RPTR_ERROR_MAX)) {
    error = -RPTR_ERROR_MAX;
  }
  /* If error is -1 we suppose this is a system error, call strerror(). */
  if (error == -1) {
    return strerror(errno);
  }

  return rptr_error_array_str[-error];
}
